package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.Orders;
import com.itheima.service.OrdersDetailService;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrdersDetailService ordersDetailService;

    // 移动端:用户下单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据: {}",orders);
        // 调用方法完成下单
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    // 移动端:用户订单分页查询
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize){
        log.info("page:{}, pageSize:{}",page,pageSize);

        //1 创建分页构造器
        Page<Orders> ordersPage = new Page<>();
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //2 创建条件构造器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        //3 添加过滤条件
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //4 添加排序条件
        wrapper.orderByDesc(Orders::getCheckoutTime);
        //5 执行查询
        ordersService.page(ordersPage,wrapper);
        //6 对象拷贝
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> list = records.stream().map((item)->{
            // 创建新对象
            OrdersDto ordersDto = new OrdersDto();
            // 对象拷贝
            BeanUtils.copyProperties(item,ordersDto);
            // 订单id
            Long id = item.getId();
            // 根据订单号查询订单详情
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            // 添加过滤条件
            queryWrapper.eq(OrderDetail::getOrderId,id);
            // 执行查询
            List<OrderDetail> detailList = ordersDetailService.list(queryWrapper);

            ordersDto.setOrderDetails(detailList);
            ordersDto.setSumNum(detailList.size());

            // 返回结果
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage);
    }

    /*// 后台端:订单明细查询
    @GetMapping("/page")
    public R<Page> page2(int page, int pageSize){
        //1 创建分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //2 创建条件构造器 -> 查询所有的订单表信息
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //3 添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //4 调用方法执行查询
        ordersService.page(ordersPage,queryWrapper);
        //5 返回执行结果
        return R.success(ordersPage);
    }*/



}
